package com.vchello.service.impl.file;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.vchello.bean.file.Folder;
import com.vchello.dao.file.FolderDao;
import com.vchello.service.file.FolderService;
import com.vchello.util.ToolUtil;

/**
 * 
 * @author power
 *
 */
@Service(value = "folderService")
public class FolderServiceImpl extends ServiceImpl<FolderDao, Folder> implements FolderService {
	
	@Autowired
	private FolderDao folderDao;

	@Override
	public List<Folder> findFolderByUserId(int userId, Folder folder) {
		Wrapper<Folder> wrapper = new EntityWrapper<>();
		wrapper.eq("userId", userId).eq("parentId", 0);
		if(ToolUtil.isNotEmpty(folder.getName())){
			wrapper.like("name", folder.getName());
		}
		return folderDao.selectList(wrapper);
	}

	@Override
	public Integer saveFolder(Folder folder) {
		return folderDao.insert(folder);
	}

	@Override
	public Folder findFolderById(int id) {
		return folderDao.selectById(id);
	}

	@Override
	public Integer updateFolder(Folder folder) {
		return folderDao.updateById(folder);
	}

	@Override
	public boolean deleteFolder(Folder folder) {
		try {
			folderDao.deleteById(folder);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public Folder findByProjectIdAndParentIdAndUserId(int projectId, int parentId, int userId) {
		Wrapper<Folder> wrapper = new EntityWrapper<>();
		wrapper.eq("proId", projectId).eq("userId", userId).eq("parentId", parentId);
		List<Folder> list = folderDao.selectList(wrapper);
		return list!=null&&list.size()>0?list.get(0):null;
	}

	@Override
	public Page<Folder> findPageFolderByUserId(Page<Folder> page, int userId, Folder folder) {
		Wrapper<Folder> wrapper = new EntityWrapper<>();
		wrapper.eq("userId", userId).eq("parentId", 0);
		if(ToolUtil.isNotEmpty(folder.getName())){
			wrapper.like("name", folder.getName());
		}
		return selectPage(page, wrapper);
	}

	@Override
	public Page<Folder> findPageChildrenFolder(Page<Folder> page, int parentId, Folder folder) {
		Wrapper<Folder> wrapper = new EntityWrapper<>();
		wrapper.eq("parentId", parentId).eq("userId", folder.getUserId());
		if(ToolUtil.isNotEmpty(folder.getName())){
			wrapper.like("name", folder.getName());
		}
		return selectPage(page, wrapper);
	}

	@Override
	public List<Folder> findFolderByParentId(int parentId) {
		Wrapper<Folder> wrapper = new EntityWrapper<>();
		return folderDao.selectList(wrapper);
	}

	@Override
	public Folder findProjectFoldByUserId(int userId) {
		Wrapper<Folder> wrapper = new EntityWrapper<>();
		wrapper.eq("userId", userId).eq("name", "其他文件");
		List<Folder> list = folderDao.selectList(wrapper);
		return list!=null&&list.size()>0?list.get(0):null;
	}

	@Override
	public Folder findProjectInvestigateFoldByUserId(int userId) {
		Wrapper<Folder> wrapper = new EntityWrapper<>();
		wrapper.eq("userId", userId).eq("name", "尽调文件");
		List<Folder> list = folderDao.selectList(wrapper);
		return list!=null&&list.size()>0?list.get(0):null;
	}

	@Override
	public List<Folder> findProjectList() {
		Wrapper<Folder> wrapper = new EntityWrapper<>();
		wrapper.eq("name", "其他文件");
		return folderDao.selectList(wrapper);
	}

	@Override
	public List<Folder> findProjectList2() {
		Wrapper<Folder> wrapper = new EntityWrapper<>();
		wrapper.eq("proId", 0).eq("parentId", 0);
		return folderDao.selectList(wrapper);
	}

	@Override
	public boolean updateFolderByProjectId(int proId, String proName) {
		return folderDao.updateFolderByProjectId(proId, proName);
	}

}
